<?php


namespace Biz\service\wikiRanking;

use Biz\model\wiki\WikiRankingBrandData;
use Biz\model\wiki\WikiRankingCateHotUp;
use Biz\model\wiki\WikiRankingCatePublic;
use Biz\service\wiki\Wiki as sWiki;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;


/**
 * 榜单后台审核相关
 * Class AdmRanking
 * @package Biz\service\wikiRanking
 */
class FullCateRanking
{

    const LIMIT = 30;
    const HOT_UP_ID = 10000;
    const PUBLIC_ID = 10001;

    const FULL_CATE_ID_MAP = [
        self::HOT_UP_ID => '热度飙升榜',
        self::PUBLIC_ID => '住友口碑榜'
    ];

    /*获取热度飙升
     *
     * */
    public static function getHopUpList($active_uid)
    {
        $rankingDay = Ranking::getRankingDay();
        $hotUpList = WikiRankingCateHotUp::getShowTop($rankingDay);
        if ($hotUpList && is_array($hotUpList)) {
            $basic_ids = [];
            $basicPicHash = [];
            foreach ($hotUpList as $val) {
                $basic_ids[] = $val['basic_id'];
                if ($val['basic_pic_id']) {
                    $basicPicHash[$val['basic_id']] = $val['basic_pic_id'];
                }
            }

            $basic_info = $basic_ids ? sWiki::getWikiListByBasicIds($basic_ids, $active_uid) : [];
            $basic_info = Ranking::HandleBasicPic($basic_info, $basicPicHash);

            $result = [];

            if ($hotUpList && is_array($hotUpList)) {
                $i = 0;
                foreach ($hotUpList as $val) {
                    if ($val['basic_id'] && $basic_info[$val['basic_id']]) {
                        $basic_info[$val['basic_id']]['score'] = round($val['score'], 1);
                        $basic_info[$val['basic_id']]['up_percent'] = $val['up_percent'];
                        $statSign = basicStatSign::genStatSign(basicStatSign::MODULE_FC_TOP_DETAIL_HEAT_RISE, [
                            basicStatSign::EXTEND_KEY_OBJ_ID => $val['basic_id'],
                            basicStatSign::EXTEND_KEY_INDEX => $i,
                            basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI_BASIC
                        ], [
                            'uid' => $active_uid,
                            'basic_id' => $val['basic_id'],
                            'brand_id' => $val['brand_id'],
                            'ranking_id' => self::HOT_UP_ID,
                        ]);
                        $basic_info[$val['basic_id']]['statSign'] = $statSign;
                        $result[] = $basic_info[$val['basic_id']];
                        $i++;
                    }
                }
            }
            return $result;
        }
    }

    /*获取住友口碑榜
     *
     * */
    public static function getPublicRecommList($active_uid)
    {
        $rankingDay = Ranking::getRankingDay();
        $list = WikiRankingCatePublic::getShowTop($rankingDay);
        $basic_ids = [];
        $basicPicHash = [];
        foreach ($list as $val) {
            $basic_ids[] = $val['basic_id'];
            if ($val['basic_pic_id']) {
                $basicPicHash[$val['basic_id']] = $val['basic_pic_id'];
            }
        }
        $basic_info = $basic_ids ? sWiki::getWikiListByBasicIds($basic_ids, $active_uid) : [];
        $basic_info = Ranking::HandleBasicPic($basic_info, $basicPicHash);
        $obj_ids = array_column($list, 'obj_id') ?: [];
        $content_info = [];
        if ($obj_ids && is_array($obj_ids)) {
            $content_info = Ranking::getContentInfo($obj_ids);
        }
        $result = [];
        if ($list && is_array($list)) {
            $i = 0;
            foreach ($list as $val) {
                if ($val['basic_id'] && $basic_info[$val['basic_id']] && $val['obj_id'] && $content_info[$val['obj_id']]) {
                    $basic_info[$val['basic_id']]['score'] = round($val['score'], 1);
                    $statSign = basicStatSign::genStatSign(basicStatSign::MODULE_FC_TOP_DETAIL_PUBLIC_PRAISE, [
                        basicStatSign::EXTEND_KEY_OBJ_ID => $val['basic_id'],
                        basicStatSign::EXTEND_KEY_INDEX => $i,
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_WIKI_BASIC
                    ], [
                        'uid' => $active_uid,
                        'basic_id' => $val['basic_id'],
                        'brand_id' => $basic_info[$val['basic_id']]['brand_id'],
                        'ranking_id' => self::PUBLIC_ID,
                        'wiki_id' => $basic_info[$val['basic_id']]['id'],

                    ]);
                    switch ($content_info[$val['obj_id']]['type']) {
                        case BlockType::TYPE_PHOTO:
                            $user_info = $content_info[$val['obj_id']]['photo']['user_info'];
                            $remark = $content_info[$val['obj_id']]['photo']['photo_info']['remark'];
                            break;
                        case BlockType::TYPE_BLANK:
                            $user_info = $content_info[$val['obj_id']]['blank']['user_info'];
                            $remark = $content_info[$val['obj_id']]['blank']['blank_info']['remark'];
                            break;
                    }
                    $result[] = [
                        'wiki_info' => $basic_info[$val['basic_id']],
                        'content_info' => [
                            'user_info' => $user_info,
                            'remark' => $remark
                        ],
                        'statSign' => $statSign,
                    ];
                    $i++;
                }
            }
        }

        return $result;

    }

    /**
     * 获取热榜推荐wiki列表
     * @param $params
     * @return array
     * @throws \Exception
     */
    public static function getTopHotWikiList($params): array
    {
        $current = $params['current_page'] * $params['per_page'];
//        $offset = ($params['current_page'] - 1) * $params['per_page'];
        $rankingDay = Ranking::getRankingDay();
        $where = [
            'ranking_state' => WikiRankingCateHotUp::RANKING_STATE_HIDE,
            'ranking_day' => $rankingDay,
            'top_num[<>]' => [30, 90],
            'ORDER' => ['top_num' => 'ASC', 'score' => 'DESC'],
        ];
        $data['is_over'] = 1;
        $data['list'] = [];
        $where['LIMIT'] = [0, 60];
        $basic_ids = WikiRankingCateHotUp::getListByWhere($where, 'basic_id') ?: [];
        $list = $basic_ids ? sWiki::getWikiListByBasicIds($basic_ids, $params['uid']) : [];
        $result = [];
        if ($basic_ids && is_array($basic_ids)) {
            foreach ($basic_ids as $val) {
                $result[] = $list[$val];
            }
        }
        $data['list'] = $result;

        return $data;
    }


    /**
     * 获取口碑榜推荐wiki列表
     * @param $params
     * @return array
     * @throws \Exception
     */
    public static function getTopPublicPraiseWikiList($params): array
    {
        $current = $params['current_page'] * $params['per_page'];
        $rankingDay = Ranking::getRankingDay();
        $where = [
            'ranking_state' => WikiRankingCatePublic::RANKING_STATE_HIDE,
            'ranking_day' => $rankingDay,
            'top_num[<>]' => [30, 90],
            'ORDER' => ['top_num' => 'ASC', 'score' => 'DESC'],
        ];
        $data['is_over'] = 1;
        $data['list'] = [];
        $where['LIMIT'] = [0, 60];
        $basic_ids = WikiRankingCatePublic::getListByWhere($where, 'basic_id') ?: [];
        $list = $basic_ids ? sWiki::getWikiListByBasicIds($basic_ids, $params['uid']) : [];
        $result = [];
        if ($basic_ids && is_array($basic_ids)) {
            foreach ($basic_ids as $val) {
                $result[] = $list[$val];
            }
        }
        $data['list'] = $result;
        return $data;
    }


    /*品牌荣耀榜
     *
     * */
    public static function getBrandGroryByBrandId($brand_id)
    {
        $rankingDay = Ranking::getRankingDay();
        $map = [
            'AND' => [
                'brand_id' => $brand_id,
                'ranking_day' => $rankingDay,
                'ranking_state' => WikiRankingCatePublic::RANKING_STATE_SHOW,
            ],
            'ORDER' => ['top_num' => 'ASC'],
            'LIMIT' => [0, 1]
        ];

        //精选榜单
        $hotUpList = WikiRankingCateHotUp::getByWhere($map);
        $publicUplist = WikiRankingCatePublic::getByWhere($map);
        //精选榜单
        $result = [];
        if ($publicUplist && is_array($publicUplist)) {
            if ($publicUplist && $publicUplist['continue_week'] >= 2) {
                $desc = '连续' . $publicUplist['continue_week'] . "周上榜";
            }
            $result[] = [
                'top_num' => $publicUplist['top_num'],
                'desc' => $desc ?: '',
                'type' => WikiRankingBrandData::TYPE_CATE_PUBLIC,
                'ranking_id' => self::PUBLIC_ID,
                'title' => '住友口碑榜'
            ];

        }
        if ($hotUpList && is_array($hotUpList)) {
            if ($hotUpList && $hotUpList['continue_week'] >= 2) {
                $desc = '连续' . $hotUpList['continue_week'] . "周上榜";
            }
            $result[] = [
                'top_num' => $hotUpList['top_num'],
                'desc' => $desc ?: '',
                'ranking_id' => self::HOT_UP_ID,
                'type' => WikiRankingBrandData::TYPE_HOT_UP,
                'title' => '热度飙升榜'
            ];
        }
        if ($result && is_array($result)) {
            $rank_list = [];
            $type_list = [];
            foreach ($result as $rank) {
                $rank_list[] = $rank['top_num'];
                $type_list[] = $rank['type'];
            }
            array_multisort($rank_list, SORT_ASC, $type_list, SORT_DESC, $result);
        }
        //排序
        return array_values($result);
    }


}